package pers.xx.sbm.config;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.xx.sbm.realm.UserRealm;
import pers.xx.sbm.utils.UserUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author ：xiexing
 * @description：shiro配置
 * @date ：2020/9/20 17:29
 */
@Configuration
public class ShiroConfig {

    @Autowired
    UserUtils userUtils;

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //以下是过滤链，按顺序过滤，所以/**需要放最后
        //开放的静态资源
        filterChainDefinitionMap.put("/**", "anon");//其他的都需要验证
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);//设置过滤链
        shiroFilterFactoryBean.setSuccessUrl("/main");//登陆成功页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/static/*");//报告页面
        shiroFilterFactoryBean.setLoginUrl("/toLogin");//设置登陆路径 (登录页面)
        shiroFilterFactoryBean.setUnauthorizedUrl("/report/*");//报告页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/reportData/*");//报告数据页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/reportDataCounty/*");//报告数据页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/dyReportData/*");//报告数据页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/dyReportCountyData/*");//报告数据页面
//        shiroFilterFactoryBean.setLoginUrl("/fileNotFound");//文件未找到
//        shiroFilterFactoryBean.setLoginUrl("/");//设置登陆路径（导航页面）
        shiroFilterFactoryBean.setUnauthorizedUrl("/toLogin");

        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(UserRealm userRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    @Bean(name = "sessionManager")
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 设置session过期时间1800s
        sessionManager.setGlobalSessionTimeout(1800000L);
        return sessionManager;
    }

    @Bean("userRealm")
    public UserRealm getUserRealm() {
        return new UserRealm();
    }

    //开启shiro aop注解支持
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
